/**
 * 
 */
$(document).ready(function() {
	var appTypeDialog;
	var bindModelData = {
		appTypeForm:{name:"",code:""},
		appTypeSearch:{name:""}
	}
	var appTypeFormModel,appTypeSearchFormModel,appTypeFormValidator;
	function init(){
		initSearchForm();
		initAppTypeForm();
		if($("#search").data("LBSFormSearch")===undefined){
			$("#search").LBSFormSearch({
				searchBar:true,
				searchHandle:searchHandle,
				resetHandle:resetSearchHandle,
				custom:[{name:"add",value:"添加",click:function(e){addAppTypeHandle(e);},spriteCssClass:"k-icon k-i-plus"}]
			});
		}
		
		if($("#appType_grid").data("kendoGrid")===undefined){
			$("#appType_grid").kendoGrid({
				autoBind:true,
		        dataSource: getGridDataSource({
		        	read:{
		        		url:"/system/appType/find",
		        	},
		        	model:{
		        		id:"id",
		        		fields:{
		        			id:{type:"string"},
		        			name:{type:"string"},
		        			code:{type:"string"}
		        		}
		        	}
		        }),
		        sortable: true,
		        selectable: true,
		        editable: false,
		        resizable: true,
		        pageable: true,
		        dataBinding: function() {
		            record = (this.dataSource.page() -1) * this.dataSource.pageSize();
		        },
		        columns: [
		            {title: "序号",template: "#= ++record #",width: 50},
		            {field: "id",title: "编号",hidden:true}, 
		            {field: "name",title: "名称"}, 
		            {field: "code",title: "编码"},
		            {title: "操作" ,command: [{name: "删除", click:deleteHandle},{name: "更新", click:modifyHandle}],width: 160}
		        ]
		    });
		}
	}
	function initSearchForm(){
		if($("input[name='name']",$("#search")).data("kendoMaskedTextBox")===undefined){
			$("input[name='name']",$("#search")).kendoMaskedTextBox();
		}
		appTypeSearchFormModel = kendo.observable(bindModelData.appTypeSearch);
		kendo.bind("#search", appTypeSearchFormModel);
	}
	function initAppTypeForm(){
		appTypeFormValidator = $("#appType_form").kendoValidator().data("kendoValidator");
		if($("#appType_form").data("LBSDialog")===undefined){
			appTypeDialog = $("#appType_form").LBSDialog({
				height:250,
				width:350,
				deactivate:resetFormHandle,
				bbars:{
					save:saveFormHandle
				}
			});
		}
		if($("input[name='name']",$("#appType_form")).data("kendoMaskedTextBox")===undefined){
			$("input[name='name']",$("#appType_form")).kendoMaskedTextBox();
		}
		if($("input[name='code']",$("#appType_form")).data("kendoMaskedTextBox")===undefined){
			$("input[name='code']",$("#appType_form")).kendoMaskedTextBox();
		}
	}
	function main(){
		init();
	}
	function searchHandle(){
		$("#appType_grid").data("kendoGrid").dataSource.read({content:kendo.stringify(appTypeSearchFormModel)});
	}
	function resetSearchHandle(){
		appTypeSearchFormModel = kendo.observable(bindModelData.appTypeSearch);
		kendo.bind("#search", appTypeSearchFormModel);
	}
	function resetFormHandle(){
		appTypeFormModel = kendo.observable(bindModelData.appTypeForm);
		kendo.bind("#appType_form", appTypeFormModel);
	}
	function saveFormHandle(){
		var url = "/system/appType/save";
		if(this.paramters!==undefined){url = this.paramters.url}
		console.log(url);
		if(appTypeFormValidator.validate()){
			httpRequestHandle({
				url:url,
				data:{content:kendo.stringify(appTypeFormModel)}
			}).done(function(response){
				$("#appType_grid").data("kendoGrid").dataSource.read();
			});
			this.close();
		}else{
			LBSNotification({message:"检查表格字段是否为空"});
		}
		delete this.paramters;
	}
	function addAppTypeHandle(){
		appTypeFormModel = kendo.observable(bindModelData.appTypeForm);
		kendo.bind("#appType_form", appTypeFormModel);
		appTypeDialog.open();
	}
	function deleteHandle(e){
		var grid = $("#appType_grid").data("kendoGrid");
		var tr = $(e.target).closest("tr");
		var data = grid.dataItem(tr);
		var id = data.id;
		if(id!==undefined){
			$.LBSOkCancelDialog({
				message:"确认删除?",
				affirm:function(data){
					if(data){
						httpRequestHandle({
							url:"/system/appType/delete",
							data:{id:id}
						}).done(function(response){
							$("#appType_grid").data("kendoGrid").dataSource.read();
						})
					}
				}
			});
		}
	}
	function modifyHandle(e){
		var grid = $("#appType_grid").data("kendoGrid");
		var tr = $(e.target).closest("tr");
		var data = grid.dataItem(tr);
		appTypeFormModel = kendo.observable(data);
		kendo.bind("#appType_form", appTypeFormModel);
		appTypeDialog.open({paramters:{url:"/system/appType/update"}});
	}
	main();
});
